import { constantRoutes } from "@/router";
import Layout from "@/views/Layout";
import { homeMenuData } from "../../utils";
import { getMenuList } from "@/router/menu.js";

const permission = {
  namespaced: true,
  state: () => ({
    routers: null,
    menuList: [],
    subMenu: [],
    currentModuleName: "main",
    currentPage: "/home",
    currentMenuTitle: "首页",
    currentTabName: "首页",
  }),
  mutations: {
    // 当前模块
    UPDATE_CURRENT_MODULE_NAME(state, payload) {
      sessionStorage.setItem("currentApp", payload);
      state.currentModuleName = payload;
    },
    // 当前页面
    UPDATE_CURRENT_PAGE(state, payload) {
      sessionStorage.setItem("currentPage", payload);
      state.currentPage = payload;
    },
    // 当前页面
    UPDATE_CURRENT_TABNAME(state, payload) {
      sessionStorage.setItem("currentTabName", payload);
      state.currentTabName = payload;
    },
    // 当前页面
    UPDATE_CURRENT_MENUTITILE(state, payload) {
      sessionStorage.setItem("currentMenuTitle", payload);
      state.currentMenuTitle = payload;
    },
    // 菜单数据
    UPDATE_MENU_LIST(state, payload) {
      state.menuList = payload;
    },
    // 左侧菜单数据，子应用菜单数据
    UPDATE_SUB_MENU(state, payload) {
      state.subMenu = [];
      if (typeof payload === "boolean") {
        state.subMenu.push(homeMenuData);
        return;
      }
      if (typeof payload === "object") {
        state.subMenu = payload;
      }
    },
    // 路由数据
    UPDATE_ROUTERS(state, payload) {
      state.routers = constantRoutes.concat(payload);
    },
  },
  actions: {
    generateRoutes({ commit }) {     
      return new Promise((resolve) => {
        const data = getMenuList();
        commit("UPDATE_MENU_LIST", data);
        let routes = [];
        const route = getMenuItem(data);
        routes = [...routes, ...route];
        routes.push({
          path: "*",
          name: "notfound",
          component: () => import("@/views/404.vue"),
          meta: { title: "404" },
        });
        commit("UPDATE_ROUTERS", routes);
        resolve(routes);
      });
    },
  },
  getters: {},
};

// 组装路由数据
function getMenuItem(menus) {
  let routers = [];
  for (let index = 0; index < menus.length; index++) {
    const menu = menus[index];
    if (menu.children && menu.children.length > 0) {
      routers = [...routers, ...getMenuItem(menu.children)];
    } else {
      const route = {
        path: menu.address,
        component: menu.moduleName === "Main" ? () => import(`@/views${menu.component}`) : Layout,
        name: menu.name,
        meta: menu.meta,
      };
      route.meta.title = menu.title;
      route.meta.moduleName = menu.moduleName;
      routers.push(route);
    }
  }
  return routers;
}

export default permission;
